package cn.yougou.serviceImpl;

import cn.yougou.mapper.YgCommoditySpecificationMapper;
import cn.yougou.pojo.YgCommoditySpecification;
import cn.yougou.service.shopcart.YgCommoditySpecificationService;
import cn.yougou.vo.YgCommoditySpecificationVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * (YgCommoditySpecification)表服务实现类
 *
 * @author 白杰崇
 * @since 2020-12-11 18:57:46
 */
@Service
public class YgCommoditySpecificationServiceImpl implements YgCommoditySpecificationService {

    @Autowired
    private YgCommoditySpecificationMapper ygCommoditySpecificationMapper;

    /**
     * 查询商品所有规格参数（加入购物车/类型） 利用Set集合实现
     * @param ygCommoditySpecification 实例对象
     * @return
     */
    @Override
    public List<YgCommoditySpecificationVo> spAll(YgCommoditySpecification ygCommoditySpecification) {
        YgCommoditySpecification commdity = null;
        YgCommoditySpecificationVo commdityVo = null;
        Set<String> spSet = new HashSet<>();
        //用于存储数据的Vo类型List
        List<YgCommoditySpecificationVo> spListVo = new ArrayList<>();
        //接收根据商品id查询回来的全部规格参数(包含了规格和颜色)
        List<YgCommoditySpecification> spList = ygCommoditySpecificationMapper.queryAll(ygCommoditySpecification);
        for(YgCommoditySpecification sp : spList){
            //利用Set值不可重复特性找出 规格名称
            if(spSet.add(sp.getSpecification())){
                //把商品id和商品规格名称放入实体类作为查询条件
                commdity = new YgCommoditySpecification(sp.getCommodityid(),sp.getSpecification());
                //接收查询回来的结果集
                List<YgCommoditySpecification> children = ygCommoditySpecificationMapper.queryAll(commdity);
                //将规格名称，规格名称对应的规格值（List）一并放入VO
                commdityVo = new YgCommoditySpecificationVo(sp.getSpecification(),children);
                spListVo.add(commdityVo);
            }
        }
        return spListVo;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public YgCommoditySpecification queryById(Integer id) {
        return this.ygCommoditySpecificationMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<YgCommoditySpecification> queryAllByLimit(int offset, int limit) {
        return this.ygCommoditySpecificationMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param ygCommoditySpecification 实例对象
     * @return 实例对象
     */
    @Override
    public YgCommoditySpecification insert(YgCommoditySpecification ygCommoditySpecification) {
        this.ygCommoditySpecificationMapper.insert(ygCommoditySpecification);
        return ygCommoditySpecification;
    }

    /**
     * 修改数据
     *
     * @param ygCommoditySpecification 实例对象
     * @return 实例对象
     */
    @Override
    public YgCommoditySpecification update(YgCommoditySpecification ygCommoditySpecification) {
        this.ygCommoditySpecificationMapper.update(ygCommoditySpecification);
        return this.queryById(ygCommoditySpecification.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.ygCommoditySpecificationMapper.deleteById(id) > 0;
    }
}